package cn.oi.book.pro.config;

import cn.oi.book.pro.model.Knowledge;
import cn.oi.book.pro.repository.KnowledgeRepository;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class DataInitializer {

    @Bean
    CommandLineRunner initData(KnowledgeRepository repo) {
        return args -> {
            if (repo.count() == 0) {
                Knowledge k1 = Knowledge.builder()
                        .title("现代Java开发最佳实践")
                        .category("Java")
                        .status("published")
                        .content("使用Spring Boot 3、JPA和记录良好编码规范，提升可维护性与性能。")
                        .build();
                Knowledge k2 = Knowledge.builder()
                        .title("MySQL性能优化指南")
                        .category("数据库")
                        .status("published")
                        .content("合理建立索引、分析慢查询、使用EXPLAIN和缓存策略。")
                        .build();
                Knowledge k3 = Knowledge.builder()
                        .title("Thymeleaf模板技巧")
                        .category("前端")
                        .status("draft")
                        .content("使用th:each、th:if、片段布局来快速构建页面。")
                        .build();
                repo.save(k1);
                repo.save(k2);
                repo.save(k3);
            }
        };
    }
}
